<?
function libraries_get($library, $version){
    if(!preg_match("/[a-zA-Z0-9_]/", $library)){
        print('{"error":"Library name incorrect."}');
        exit();
    }
    if(!preg_match("/([0-9]*)\.([0-9]*)\.([0-9]*)/", $version, $vers)){
        print('{"error":"Version incorrect."}');
        exit();
    }
    
    ob_start();
    
    $f=fopen("http://download.symbiosis.webex.am/libraries/".$library.".lib", "r");
    fgets($f);
    $line=substr(fgets($f), 2);
    preg_match("/[a-zA-Z0-9 ]* ([0-9]*)\.([0-9]*)\.([0-9]*)/", $line, $pattern);
    
    $f=fopen("http://download.symbiosis.webex.am/libraries/".$library.".lib", "r");
    
    if(ob_get_clean()==''){
        $t=0;
        for($i=1; $i<=3; $i++){
            if($vers[$i]>$pattern[$i]){
                $t=-1;
                break;
            }
            if($vers[$i]<$pattern[$i]){
                $t=1;
                break;
            }
        }
        $content='';
        while($buf=fread($f, 8192)){
            $content.=$buf;
        }
        if($t==1){
            ob_start();
            $f=fopen("../../../libraries/".$library.".lib.php", "w");
            if(ob_get_clean()==''){
                if(fwrite($f, $content)){
                    print('{"ok":"Updated", "version":"'.$pattern[1].'.'.$pattern[2].'.'.$pattern[3].'"}');
                }
                else{
                    print('{"error":"Can\'t write file."}');
                }
            }
            else{
                print('{"error":"Can\'t open file."}s');
            }
        }
        elseif($t==-1){
            print('{"ok":"You have the lasted version", "version":"'.$vers[1].'.'.$vers[2].'.'.$vers[3].'"}');
        }
        else{
            print('{"ok":"You have the last version", "version":"'.$pattern[1].'.'.$pattern[2].'.'.$pattern[3].'"}');
        }
    }
    else{
        print('{"error":"File not exists."}');
        exit();
    }
}

function symbionts_get($symbiont, $version){
    try{
        if(!preg_match("/[a-zA-Z0-9_]/", $symbiont)){
            throw new Exception("Symbiont name incorrect.");
        }
        if(!preg_match("/([0-9]*)\.([0-9]*)\.([0-9]*)/", $version, $vers)){
            throw new Exception("Version incorrect.");
        }
        
        ob_start();
        $f=fopen("http://download.symbiosis.webex.am/symbionts_json/".$symbiont.".json", "r");
        $content='';
        while($buf=fread($f, 8192)){
            $content.=$buf;
        }
        $json=json_decode($content);
        preg_match("/([0-9]*)\.([0-9]*)\.([0-9]*)/", $json->version, $v);
        $f=fopen("http://download.symbiosis.webex.am/symbionts/".$symbiont.".zip", "r");
        
        if(ob_get_clean()!=''){
            throw new Exception("File not exists.");
        }
        $t=0;
        for($i=1; $i<=3; $i++){
            if($vers[$i]>$v[$i]){
                $t=1;
                break;
            }
            if($vers[$i]<$v[$i]){
                $t=-1;
                break;
            }
        }
        if($t==-1){
            
            include('../../../config.php');
            mysql_connect($host, $user, $pass) or die ("Couldn't connect to database.");
            mysql_query('SET NAMES utf8');
            mysql_select_db($db) or die ("Couldn't select the database.");
            
            
            $content='';
            while($buf=fread($f, 8192)){
                $content.=$buf;
            }
            ob_start();
            $f=fopen("../../../temp/".$symbiont.".zip", "w");
            if(ob_get_clean()!=''){
                throw new Exception("Can\'t open file.");
            }
            if(!fwrite($f, $content)){
                throw new Exception("Can\'t write file.");
            }
            $designs=array();
            $dir=opendir('../../../design');
            while($d=readdir($dir)){
                if($d=='.'||$d=='..') continue;
                if(!is_dir('../../../design/'.$d.'/')) continue;
                $designs[]=$d;
            }
            $zip = zip_open("../../../temp/".$symbiont.".zip");
            if(!is_resource($zip)){
                throw new Exception("Can\'t open archive.");
            }
            while($zip_entry = zip_read($zip)){
                $name=zip_entry_name($zip_entry);
                $des="design/";
                $sym="symbiont/";
                $t=0;
                if(substr($name, 0, strlen($des))==$des){
                    $t=1;
                }
                elseif(substr($name, 0, strlen($sym))==$sym){
                    $t=2;
                }
                if(substr($name, -1)=='/'){
                    if($t==1){
                        $x=substr($name, strlen($des));
                        foreach($designs as $d){
                            $new_name='../../../'.$des.$d.'/'.$x;
                            if(!file_exists($new_name)) mkdir($new_name, 0755);
                        }
                    }
                    elseif($t==2){
                        $x=substr($name, strlen($sym));
                        $new_name='../../../symbionts/'.$symbiont.'/'.$x;
                        if(!file_exists($new_name)) mkdir($new_name, 0755);
                    }
                    else{
                        $new_name='../../../'.$name;
                        if(!file_exists($new_name)) mkdir($new_name, 0755);
                    }
                }
                else{
                    if(!zip_entry_open($zip, $zip_entry, "r")){
                        throw new Exception("Can\'t entry archive.");
                    }
                    $buf=zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                    if($t==1){
                        $x=substr($name, strlen($des));
                        foreach($designs as $d){
                            $file='../../../'.$des.$d.'/'.$x;
                            $f=fopen($file, "w");
                            fwrite($f, $buf);
                        }
                    }
                    elseif($t==2){
                        $x=substr($name, strlen($sym));
                        $file='../../../symbionts/'.$symbiont.'/'.$x;
                        $f=fopen($file, "w");
                        fwrite($f, $buf);
                    }
                    else{
                        if($name=="db.sql"){
                            $sql=explode(';\n', $buf);
                            foreach($sql as $val){
                                mysql_query($val);
                            }
                        }
                        elseif($name=="labels.json"){
                            $json=json_decode($buf);
                            $q=mysql_query("SELECT `id`, `abbr` FROM `languages`");
                            while($row=mysql_fetch_assoc($q)){
                                $lng[$row['abbr']]=$row['id'];
                            }
                            foreach($json as $val){
                                $label=$val->label;
                                foreach($val->values as $k=>$vl){
                                    if(!isset($id)){
                                        mysql_query("INSERT INTO `labels` (`name`, `lng_id`, `value`) VALUES ('".$label."', '".$lng[$k]."', '".$vl."')");
                                        $id=mysql_insert_id();
                                    }
                                    else{
                                        mysql_query("INSERT INTO `labels` (`id`, `name`, `lng_id`, `value`) VALUES ('".$id."', '".$label."', '".$lng[$k]."', '".$vl."')");
                                    }
                                }
                            }
                        }
                        else{
                            $file='../../../'.$name;
                            $f=fopen($file, "w");
                            fwrite($f, $buf);
                        }
                    }
                    zip_entry_close($zip_entry);
                }
            }
            zip_close($zip);
            print('{"ok":"Updated", "version":"'.$v[1].'.'.$v[2].'.'.$v[3].'"}');
        }
        elseif($t==1){
            print('{"ok":"You have the lasted version", "version":"'.$vers[1].'.'.$vers[2].'.'.$vers[3].'"}');
        }
        else{
            print('{"ok":"You have the last version", "version":"'.$v[1].'.'.$v[2].'.'.$v[3].'"}');
        }
    }
    catch(Exception $e){
        print('{"error":"'.$e->getMessage().'"}');
    }
}
?>